package com.woniuxy.dynamic.cglib;

import com.woniuxy.dynamic.aspect.LogForService;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;


import java.lang.reflect.Method;

/**
 * @author: wgt
 * Date: 2021/11/15:15:11
 * Description:
 */
public class CglibDynamic {
    public Object getProxyObject(Object targetClass){
        Object o = Enhancer.create(targetClass.getClass(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                LogForService logForService = new LogForService();
                logForService.beforeStrengthen();
                Object invoke = method.invoke(targetClass, args);
                logForService.afterStrengthen();
                return invoke;
            }
        });
        return o;
    }
}
